/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewMenuPanel.java
 *
 * Created on Apr 8, 2010, 7:07:58 PM
 */

package edu.asu.cse360.client.ui.frame;
import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.client.util.CustomerConfiguration;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.model.IngredientQuantity;
import edu.asu.cse360.model.ItemOrder;
import edu.asu.cse360.model.MenuItem;
import edu.asu.cse360.model.Order;
import edu.asu.cse360.model.OrderCustomization;
import edu.asu.cse360.model.Photo;
import edu.asu.cse360.service.ItemOrderService;
import edu.asu.cse360.service.MenuItemService;
import edu.asu.cse360.service.OrderService;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.AbstractListModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import edu.asu.cse360.client.util.Formatter;
/**
 *This class is to display the menu for the customer
 * @author owner
 */
public class ViewMenuPanel extends javax.swing.JPanel {

    private static ViewMenuPanel instance;

    public static ViewMenuPanel instance(CustomerConfiguration config) {
        if(instance == null){
            instance = new ViewMenuPanel(config);
        }
        return instance;
    }
    private MenuItemModel menuItems;
    private ItemOrderModel orderItems;
    private DefaultComboBoxModel saltItems;
    private DefaultComboBoxModel sugarItems;
    private MenuItemService itemSvc;
    private ItemOrderService orderItemSvc;
    private OrderService orderSvc;
    private Photo photo;
    private CustomerConfiguration customerConfiguration;
    /** Creates new form ViewMenuPanel */
    private ViewMenuPanel(CustomerConfiguration customerConfiguration) {
        this.customerConfiguration = customerConfiguration;
        itemSvc = ServiceFactory.getMenuItemService();
        orderItemSvc = ServiceFactory.getItemOrderService();
        orderSvc = ServiceFactory.getOrderService();
        menuItems = new MenuItemModel();
        orderItems = new ItemOrderModel();
        saltItems = new DefaultComboBoxModel(IngredientQuantity.values());
        sugarItems = new DefaultComboBoxModel(IngredientQuantity.values());
        initComponents();
        refreshListModel();
        customizationPanel.setVisible(false);
    }

     private ListModel getMenuItemModel(){
         return menuItems;
    }

    public void refresh() {
        this.refreshListModel();
    }

    private void refreshListModel() {
       List<MenuItem> availableItems = itemSvc.getAvailableMenuItems(RestaurantContext.getRestaurant());
       menuItems.setMenuItems(availableItems);
    }

    public void reset() {
        refresh();
        this.orderItems.clear();
    }

    private class MenuItemModel extends AbstractListModel{

      private List<MenuItem> menuItems;

      public MenuItemModel(){
          this(null);
      }

      public MenuItemModel(List<MenuItem> newItems){
          this.menuItems = newItems == null ? new ArrayList<MenuItem>() : newItems;
      }

        //Override the methods in the abstract class.
      public int getSize()
      { return menuItems.size(); }

      public Object getElementAt(int index)
      { return menuItems.get(index).getName(); }

      public MenuItem getMenuItemAt(int index)
      {
          return menuItems.get(index);
      }

        private void setMenuItems(List<MenuItem> availableItems) {
            this.menuItems = availableItems;
        }

      }
    private ListModel getOrderItemModel(){
         return orderItems;
    }
    public void setOrderItemModel(ItemOrderModel orderItems){
        this.orderItems = orderItems;
    }
    private class ItemOrderModel extends AbstractListModel{

      protected List<ItemOrder> orderItems;

 
      public ItemOrderModel(List<ItemOrder> newOrders)
      {
            orderItems = new ArrayList<ItemOrder>();
            if(newOrders != null){
                orderItems.addAll(newOrders);
            }
      }

        private ItemOrderModel() {
            this(null);
        }

        //Override the methods in the abstract class.
      public int getSize()
      { return orderItems.size(); }

      public List<ItemOrder> getItemOrders()
      {
          return orderItems;
      }

      public Object getElementAt(int index)
      { return orderItems.get(index).getMenuItem().getName(); }

      public void addItem(ItemOrder item)
      {
          orderItems.add(item);
      }
      public void removeItem(ItemOrder item)
      {
          orderItems.remove(item);
      }

      public ItemOrder getOrderItemAt(int index)
      {
          return orderItems.get(index);
      }

        private void clear() {
            this.orderItems.clear();
            currentOrderList.updateUI();
        }

      }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        itemDescriptionPanel = new javax.swing.JPanel();
        Description = new javax.swing.JLabel();
        itemDescriptionLbl = new javax.swing.JLabel();
        picLbl = new javax.swing.JLabel();
        itemNameLbl = new javax.swing.JLabel();
        itemRatingLbl = new javax.swing.JLabel();
        itemPriceLbl = new javax.swing.JLabel();
        PlaceOrder = new javax.swing.JButton();
        lblCookTime = new javax.swing.JLabel();
        Menu = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        menuItemsList = new javax.swing.JList();
        MenuItems = new javax.swing.JLabel();
        Custom = new javax.swing.JPanel();
        Add = new javax.swing.JButton();
        customizationPanel = new javax.swing.JPanel();
        saltCombo = new javax.swing.JComboBox();
        sugarCombo = new javax.swing.JComboBox();
        saltLbl = new javax.swing.JLabel();
        sugarLbl = new javax.swing.JLabel();
        customizeBtn = new javax.swing.JButton();
        notesLbl = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        notesTxt = new javax.swing.JTextArea();
        placeOrderPanel = new javax.swing.JPanel();
        CurrentItem = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        currentOrderList = new javax.swing.JList();
        removeButton = new javax.swing.JButton();

        Description.setText("Item Description");

        itemDescriptionLbl.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        itemDescriptionLbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        itemNameLbl.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        itemNameLbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        itemRatingLbl.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        itemRatingLbl.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        PlaceOrder.setText("Place Order");
        PlaceOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlaceOrderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout itemDescriptionPanelLayout = new javax.swing.GroupLayout(itemDescriptionPanel);
        itemDescriptionPanel.setLayout(itemDescriptionPanelLayout);
        itemDescriptionPanelLayout.setHorizontalGroup(
            itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemDescriptionPanelLayout.createSequentialGroup()
                .addGroup(itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(itemDescriptionPanelLayout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(Description))
                    .addGroup(itemDescriptionPanelLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(PlaceOrder)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(picLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(itemDescriptionLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                            .addComponent(itemPriceLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                            .addComponent(itemNameLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                            .addGroup(itemDescriptionPanelLayout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addGroup(itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblCookTime, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                                    .addComponent(itemRatingLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        itemDescriptionPanelLayout.setVerticalGroup(
            itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemDescriptionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Description)
                .addGap(18, 18, 18)
                .addGroup(itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(itemDescriptionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(itemDescriptionPanelLayout.createSequentialGroup()
                            .addComponent(itemNameLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(itemDescriptionLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lblCookTime, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(itemRatingLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(itemPriceLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addComponent(picLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(PlaceOrder)))
        );

        menuItemsList.setModel(getMenuItemModel());
        menuItemsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                menuItemsListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(menuItemsList);

        MenuItems.setText("Menu Items");

        Add.setText("Add To Order");
        Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddActionPerformed(evt);
            }
        });

        saltCombo.setModel(saltItems);
        saltCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saltComboActionPerformed(evt);
            }
        });

        sugarCombo.setModel(sugarItems);

        saltLbl.setText("Salt:");

        sugarLbl.setText("Sugar:");

        customizeBtn.setText("Apply Customizations");
        customizeBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customizeBtnActionPerformed(evt);
            }
        });

        notesLbl.setText("Extra Notes:");

        notesTxt.setColumns(20);
        notesTxt.setRows(5);
        jScrollPane3.setViewportView(notesTxt);

        javax.swing.GroupLayout customizationPanelLayout = new javax.swing.GroupLayout(customizationPanel);
        customizationPanel.setLayout(customizationPanelLayout);
        customizationPanelLayout.setHorizontalGroup(
            customizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customizationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(customizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(saltCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(customizationPanelLayout.createSequentialGroup()
                        .addGroup(customizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(sugarLbl)
                            .addComponent(saltLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(sugarCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(148, 148, 148))
            .addGroup(customizationPanelLayout.createSequentialGroup()
                .addGap(113, 113, 113)
                .addComponent(notesLbl)
                .addContainerGap(181, Short.MAX_VALUE))
            .addGroup(customizationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, customizationPanelLayout.createSequentialGroup()
                .addContainerGap(143, Short.MAX_VALUE)
                .addComponent(customizeBtn)
                .addGap(77, 77, 77))
        );
        customizationPanelLayout.setVerticalGroup(
            customizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customizationPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(customizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saltCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saltLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(customizationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sugarCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sugarLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addComponent(notesLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(customizeBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout CustomLayout = new javax.swing.GroupLayout(Custom);
        Custom.setLayout(CustomLayout);
        CustomLayout.setHorizontalGroup(
            CustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, CustomLayout.createSequentialGroup()
                .addContainerGap(147, Short.MAX_VALUE)
                .addComponent(Add)
                .addGap(121, 121, 121))
            .addGroup(CustomLayout.createSequentialGroup()
                .addComponent(customizationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        CustomLayout.setVerticalGroup(
            CustomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(CustomLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(Add)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(customizationPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout MenuLayout = new javax.swing.GroupLayout(Menu);
        Menu.setLayout(MenuLayout);
        MenuLayout.setHorizontalGroup(
            MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MenuLayout.createSequentialGroup()
                .addGroup(MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MenuLayout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(MenuItems))
                    .addGroup(MenuLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(MenuLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Custom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        MenuLayout.setVerticalGroup(
            MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MenuLayout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addComponent(MenuItems, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Custom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        CurrentItem.setText("Current Item");

        currentOrderList.setModel(getOrderItemModel());
        currentOrderList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                currentOrderListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(currentOrderList);

        removeButton.setText("Remove From Order");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout placeOrderPanelLayout = new javax.swing.GroupLayout(placeOrderPanel);
        placeOrderPanel.setLayout(placeOrderPanelLayout);
        placeOrderPanelLayout.setHorizontalGroup(
            placeOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(placeOrderPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(placeOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(placeOrderPanelLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(CurrentItem))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(placeOrderPanelLayout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(removeButton)))
                .addContainerGap())
        );
        placeOrderPanelLayout.setVerticalGroup(
            placeOrderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, placeOrderPanelLayout.createSequentialGroup()
                .addComponent(CurrentItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(removeButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(Menu, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(itemDescriptionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(placeOrderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(itemDescriptionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(placeOrderPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(Menu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddActionPerformed
        MenuItem selectedItem = (MenuItem)menuItems.getMenuItemAt(menuItemsList.getSelectedIndex());
        ItemOrder itemOrder = new ItemOrder();
        itemOrder.setMenuItem(selectedItem);
        OrderCustomization orderCustom = new OrderCustomization();
        itemOrder.setCustomization(orderCustom);
        orderItems.addItem(itemOrder);
        currentOrderList.updateUI();
}//GEN-LAST:event_AddActionPerformed
private void updatePicLbl(Photo photo) {
        if(photo != null){
            ImageIcon icon;
            try {
                String url = photo.getUrl();
                BufferedImage bufferedImage;
                if(url.startsWith("/")){
                    bufferedImage = ImageIO.read(new File(photo.getUrl()));
                }else{
                    bufferedImage = ImageIO.read(new URL(photo.getUrl()));
                }
                icon = new ImageIcon(bufferedImage);
                picLbl.setIcon(icon);
                picLbl.setText("");
            } catch (IOException ex) {
                picLbl.setIcon(null);
                picLbl.setText("Error while loading Image");
            }

        }else{
            picLbl.setIcon(null);
            picLbl.setText("No Photo");
        }
    }
    private void menuItemsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_menuItemsListValueChanged
        MenuItem selectedItem = (MenuItem)menuItems.getMenuItemAt(menuItemsList.getSelectedIndex());
        photo = selectedItem.getPhoto();
        updatePicLbl(photo);
        String priceString = "$"+selectedItem.getPrice().toString();
        String cookString = Formatter.format(selectedItem.getTimeToCook());
        String ratingStr = selectedItem.getRating() == null ? "N/A" : selectedItem.getRating().toString();
        String name = "Name: " + selectedItem.getName();
        String description = "Description: " + selectedItem.getDescription();
        String rating = "Rating: " + ratingStr;
        String price = "Price: " + priceString;
        String cookTime = "Cook Time: " + cookString;
        itemNameLbl.setText(name);
        itemDescriptionLbl.setText(description);
        itemRatingLbl.setText(rating);
        itemPriceLbl.setText(price);
        lblCookTime.setText(cookTime);

}//GEN-LAST:event_menuItemsListValueChanged

    private void customizeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customizeBtnActionPerformed
        ItemOrder selectedItem =  (ItemOrder)orderItems.getOrderItemAt(currentOrderList.getSelectedIndex());
        selectedItem.getCustomization().setSaltQuantity((IngredientQuantity)saltCombo.getSelectedItem());
        selectedItem.getCustomization().setSugarQuantity((IngredientQuantity)sugarCombo.getSelectedItem());
        selectedItem.getCustomization().setNotes(notesTxt.getText());
}//GEN-LAST:event_customizeBtnActionPerformed

    private void PlaceOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlaceOrderActionPerformed

        if(!orderItems.getItemOrders().isEmpty()){
            int exit = JOptionPane.showConfirmDialog(null, "Are you sure you want to place order?", "Place Order", JOptionPane.OK_CANCEL_OPTION);
            if(exit == JOptionPane.OK_OPTION) {
                Order order = new Order(orderItems.getItemOrders(), RestaurantContext.getTable());
                order = orderSvc.placeOrder(order, RestaurantContext.getSession());
                RestaurantContext.setSession(order.getSession());
                customerConfiguration.postPlaceOrder(order);
            }
        }
        else
            JOptionPane.showMessageDialog(null, "Unable to place order, your order is currently empty", "Place Order", JOptionPane.OK_CANCEL_OPTION);
}//GEN-LAST:event_PlaceOrderActionPerformed

    private void currentOrderListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_currentOrderListValueChanged
        ItemOrder selectedItem =  (ItemOrder)orderItems.getOrderItemAt(currentOrderList.getSelectedIndex());
        customizationPanel.setVisible(true);
        saltCombo.setSelectedItem(selectedItem.getCustomization().getSaltQuantity());
        sugarCombo.setSelectedItem(selectedItem.getCustomization().getSugarQuantity());
        notesTxt.setText(selectedItem.getCustomization().getNotes());
}//GEN-LAST:event_currentOrderListValueChanged

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        ItemOrder selectedItem =  (ItemOrder)orderItems.getOrderItemAt(currentOrderList.getSelectedIndex());
        orderItems.removeItem(selectedItem);
        currentOrderList.updateUI();
        if(orderItems.getSize() == 0) {
            customizationPanel.setVisible(false);
        } else {
            selectedItem = (ItemOrder)orderItems.getOrderItemAt(currentOrderList.getSelectedIndex());
            saltCombo.setSelectedItem(selectedItem.getCustomization().getSaltQuantity());
            sugarCombo.setSelectedItem(selectedItem.getCustomization().getSugarQuantity());
            notesTxt.setText(selectedItem.getCustomization().getNotes());
        }
    }//GEN-LAST:event_removeButtonActionPerformed

    private void saltComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saltComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_saltComboActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add;
    private javax.swing.JLabel CurrentItem;
    private javax.swing.JPanel Custom;
    private javax.swing.JLabel Description;
    private javax.swing.JPanel Menu;
    private javax.swing.JLabel MenuItems;
    private javax.swing.JButton PlaceOrder;
    private javax.swing.JList currentOrderList;
    private javax.swing.JPanel customizationPanel;
    private javax.swing.JButton customizeBtn;
    private javax.swing.JLabel itemDescriptionLbl;
    private javax.swing.JPanel itemDescriptionPanel;
    private javax.swing.JLabel itemNameLbl;
    private javax.swing.JLabel itemPriceLbl;
    private javax.swing.JLabel itemRatingLbl;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblCookTime;
    private javax.swing.JList menuItemsList;
    private javax.swing.JLabel notesLbl;
    private javax.swing.JTextArea notesTxt;
    private javax.swing.JLabel picLbl;
    private javax.swing.JPanel placeOrderPanel;
    private javax.swing.JButton removeButton;
    private javax.swing.JComboBox saltCombo;
    private javax.swing.JLabel saltLbl;
    private javax.swing.JComboBox sugarCombo;
    private javax.swing.JLabel sugarLbl;
    // End of variables declaration//GEN-END:variables

}
